package com.jtjrenren.android.taxi.passenger.core.setting.address;

import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.core.order.place.PlaceChooseActivity;
import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.db.dao.AddressDao;
import com.jtjrenren.android.taxi.passenger.bean.AddressInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/1/26.
 */
public class SetAddressActivity extends BaseActivity {
    private static final int REQUEST_CODE_HOME = 400;
    private static final int REQUEST_CODE_COMPANY = 401;
    private static final int REQUEST_CODE_OTHER = 402;

    @BindView(R.id.addr_used_home_desc)
    TextView tv_home_desc;
    @BindView(R.id.addr_used_company_desc)
    TextView tv_company_desc;
    @BindView(R.id.addr_used_other_desc)
    TextView tv_other_desc;
    @BindView(R.id.title)
    TextView tv_title;


    private int currentRequeCode;

    @Override
    public int getLayoutId() {
        return R.layout.aty_setting_setaddr;
    }

    @Override
    public void init() {
        tv_title.setText("设置地址");
        refreshAddrsView();
    }


    private void refreshAddrsView() {
        AddressDao dao = new AddressDao(this);
        List<AddressInfo> list = dao.getAddrListByType(Constants.CONFIG_ADDR_TYPE_COMPANY);
        if (list.size() > 0) {
            tv_company_desc.setText(list.get(0).getName());
            list.clear();
        }
        list = dao.getAddrListByType(Constants.CONFIG_ADDR_TYPE_HOME);
        if (list.size() > 0) {
            tv_home_desc.setText(list.get(0).getName());
            list.clear();
        }
        list = dao.getAddrListByType(Constants.CONFIG_ADDR_TYPE_OTHER);
        if (list.size() > 0) {
            tv_other_desc.setText(list.get(0).getName());
            list.clear();
        }
        list = null;
        dao = null;
    }

    @OnClick(R.id.title_left)
    public void clickBack() {
        finish();
    }

    @OnClick(R.id.addr_used_home)
    public void clickHome() {
        goChooseAddrPage(REQUEST_CODE_HOME);
    }

    @OnClick(R.id.addr_used_company)
    public void clickCompany() {
        goChooseAddrPage(REQUEST_CODE_COMPANY);
    }

    @OnClick(R.id.addr_used_other)
    public void clickOther() {
        goChooseAddrPage(REQUEST_CODE_OTHER);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_HOME:
            case REQUEST_CODE_COMPANY:
            case REQUEST_CODE_OTHER:
                if (resultCode == Activity.RESULT_OK) {
                    AddressInfo info = (AddressInfo) data.getSerializableExtra(PlaceChooseActivity.INTENT_DATA_ADDR);
                    int type = Constants.CONFIG_ADDR_TYPE_OTHER;
                    if (currentRequeCode == REQUEST_CODE_HOME) {
                        type = Constants.CONFIG_ADDR_TYPE_HOME;
                        tv_home_desc.setText(info.getName());
                    } else if (currentRequeCode == REQUEST_CODE_COMPANY) {
                        type = Constants.CONFIG_ADDR_TYPE_COMPANY;
                        tv_company_desc.setText(info.getName());
                    } else {
                        tv_other_desc.setText(info.getName());
                    }
                    info.setType(type);
                    AddressDao dao = new AddressDao(this);
                    dao.addNewAddress(info);
                    dao = null;
                }
                break;
        }
    }

    private void goChooseAddrPage(int requestCode) {
        currentRequeCode = requestCode;
        Intent intent = new Intent(this, PlaceChooseActivity.class);
        startActivityForResult(intent, requestCode);
    }
}
